//
//  HMessageManageVC.m
//  BXlive
//
//  Created by k on 2018/4/17.
//  Copyright © 2018年 cat. All rights reserved.
//

#import "HMessageManageVC.h"
#import "HMessageManageCell.h"
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <Masonry/Masonry.h>
#import <SDAutoLayout/SDAutoLayout.h>

@interface HMessageManageVC () <UITableViewDelegate,UITableViewDataSource,HMessageManageDelegate>

@property(nonatomic,strong)UIView *navigationView;
@property (nonatomic,strong) UITableView *mainTableView;//表单视图
@property (nonatomic,strong) NSMutableArray *firstTitleArray;
@property (nonatomic,strong) NSMutableArray *secondTitleArray;
@property (nonatomic,strong) NSMutableArray *firstNameArray;
@property (nonatomic,strong) NSMutableArray *secondNameArray;
@property  (strong, nonatomic) NSMutableArray *subTitles;

@end

@implementation HMessageManageVC

- (void)getSubTitles {
    BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
    _subTitles = [@[liveUser.comment_push,liveUser.like_push,liveUser.follow_push,liveUser.at_push,liveUser.follow_new_push,liveUser.recommend_push,liveUser.follow_live_push,liveUser.msg_push] mutableCopy];
   
}

- (NSMutableArray *)firstTitleArray
{
    if (!_firstTitleArray) {
        _firstTitleArray = [NSMutableArray arrayWithObjects:NSLocalizedString(@"评论", nil),NSLocalizedString(@"点赞", nil),NSLocalizedString(@"关注", nil),NSLocalizedString(@"@我的", nil), nil];
    }
    return _firstTitleArray;
}

- (NSMutableArray *)secondTitleArray
{
    if (!_secondTitleArray) {
        _secondTitleArray = [NSMutableArray arrayWithObjects:NSLocalizedString(@"关注的人发布新视频", nil),NSLocalizedString(@"推荐视频", nil),NSLocalizedString(@"关注的人开播", nil), nil];
        
    }
    return _secondTitleArray;
}

- (NSMutableArray *)firstNameArray
{
    if (!_firstNameArray) {
        _firstNameArray = [NSMutableArray arrayWithObjects:@"comment_push",@"like_push",@"follow_push", nil];
    }
    return _firstNameArray;
}

- (NSMutableArray *)secondNameArray
{
    if (!_secondNameArray) {
        _secondNameArray = [NSMutableArray arrayWithObjects:@"follow_new_push",@"recommend_push",@"follow_live_push",@"msg_push", nil];
    }
    return _secondNameArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationItem.title = NSLocalizedString(@"通知管理", nil);
    self.fd_prefersNavigationBarHidden = YES;
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    [self setupNavigationView];
    [self getSubTitles];
    [self createTableView];
}
-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"通知管理", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
}

#pragma mark - 创建表单视图
- (void)createTableView
{
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.backgroundColor = [UIColor clearColor];
    self.mainTableView.showsVerticalScrollIndicator = YES;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.mainTableView];
    WS(weakSelf);
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.equalTo(weakSelf.navigationView.mas_bottom);
    }];
    
}
#pragma mark - UITableView delegate/dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.firstTitleArray.count;
    }
    return self.secondTitleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return __ScaleWidth(61);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMessageManageCell *cell = [HMessageManageCell cellWithTabelView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.titleLabel.text = self.firstTitleArray[indexPath.row];
        cell.fengeView.hidden = (indexPath.row == self.firstTitleArray.count-1);
    }
    if (indexPath.section == 1) {
        cell.titleLabel.text = self.secondTitleArray[indexPath.row];
        cell.fengeView.hidden = (indexPath.row == self.secondTitleArray.count-1);
    }
    cell.manageSwitch.selected = [_subTitles[indexPath.section * 4 + indexPath.row] integerValue];
    cell.messageManageDelegate = self;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
#pragma mark - 表头
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return __ScaleWidth(22+15);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *ID = @"manageHeader";
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (header == nil) {
        header = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
        header.contentView.backgroundColor = sl_BGColors;
        UILabel *label = [UILabel initWithFrame:CGRectZero size:14 color:sl_textColors alignment:NSTextAlignmentLeft lines:1];
        label.font = SLBFont(__ScaleWidth(16));
        
        [header.contentView addSubview:label];
        label.sd_layout.leftSpaceToView(header.contentView, __ScaleWidth(12)).topSpaceToView(header.contentView, __ScaleWidth(15)).bottomSpaceToView(header.contentView, 0).rightSpaceToView(header.contentView, 15);
        
        if (section == 0) {
            label.text = NSLocalizedString(@"消息通知", nil);
        }else{
            label.text = NSLocalizedString(@"其它通知", nil);
        }
    }
    return header;
}

#pragma - mark HMessageManageDelegate
- (void)switchClick:(UIButton *)sender cell:(UITableViewCell *)cell {
    NSIndexPath *indexPath = [self.mainTableView indexPathForCell:cell];
    if (!indexPath) {
        return;
    }
    
    NSString *value;
    if (sender.selected) {
        value = @"1";
    }else {
        value = @"0";
    }

    NSString *name = nil;
    if (indexPath.section == 0) {
        name = self.firstNameArray[indexPath.row];
    } else {
        name = self.secondNameArray[indexPath.row];
    }
    
    [[NewHttpRequestHuang sharedNewHttpRequestHuang] UserSwitchStatusWithName:name Value:value Success:^(id responseObject) {
        if ([responseObject[@"code"]intValue]==0) {
            NSString *status = [NSString stringWithFormat:@"%@",responseObject[@"data"][name]];
            BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
            [liveUser setValue:[NSString stringWithFormat:@"%@",responseObject[@"data"][name]] forKey:name];
            [BXLiveUser setCurrentBXLiveUser:liveUser];
            
            [self.subTitles replaceObjectAtIndex:(indexPath.section * 4 + indexPath.row) withObject:status];
            [self.mainTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        } else {
            [self.mainTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [BGProgressHUD showInfoWithMessage:responseObject[@"msg"]];
        }
    } Failure:^(NSError *error) {
        [self.mainTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"设置失败", nil)];
    }];
}

@end
